home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / WaveTableOscControl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  65.2 KB  |  1,743 lines  |  [TEXT/KAHL]

  1. /* WaveTableOscControl.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "WaveTableOscControl.h"
  31. #include "FastFixedPoint.h"
  32. #include "Memory.h"
  33. #include "EnvelopeState.h"
  34. #include "SampleConsts.h"
  35. #include "LFOGenerator.h"
  36. #include "Multisampler.h"
  37. #include "OscillatorSpecifier.h"
  38. #include "Envelope.h"
  39. #include "LFOListSpecifier.h"
  40. #include "FastModulation.h"
  41. #include "64BitMath.h"
  42. #include "ErrorDaemon.h"
  43.  
  44.  
  45. /* prototypes for fast playback routines */
  46. static void                        Wave_Mono_8BitIn_NoTime_NoWave(WaveTableStateRec* State,
  47.                                                 long SampleCount, largefixedsigned* RawBuffer);
  48. static void                        Wave_Stereo_8BitIn_NoTime_NoWave(WaveTableStateRec* State,
  49.                                                 long SampleCount, largefixedsigned* RawBuffer);
  50. static void                        Wave_Mono_16BitIn_NoTime_NoWave(WaveTableStateRec* State,
  51.                                                 long SampleCount, largefixedsigned* RawBuffer);
  52. static void                        Wave_Stereo_16BitIn_NoTime_NoWave(WaveTableStateRec* State,
  53.                                                 long SampleCount, largefixedsigned* RawBuffer);
  54.  
  55. static void                        Wave_Mono_8BitIn_YesTime_NoWave(WaveTableStateRec* State,
  56.                                                 long SampleCount, largefixedsigned* RawBuffer);
  57. static void                        Wave_Stereo_8BitIn_YesTime_NoWave(WaveTableStateRec* State,
  58.                                                 long SampleCount, largefixedsigned* RawBuffer);
  59. static void                        Wave_Mono_16BitIn_YesTime_NoWave(WaveTableStateRec* State,
  60.                                                 long SampleCount, largefixedsigned* RawBuffer);
  61. static void                        Wave_Stereo_16BitIn_YesTime_NoWave(WaveTableStateRec* State,
  62.                                                 long SampleCount, largefixedsigned* RawBuffer);
  63.  
  64. static void                        Wave_Mono_8BitIn_YesTime_YesWave(WaveTableStateRec* State,
  65.                                                 long SampleCount, largefixedsigned* RawBuffer);
  66. static void                        Wave_Stereo_8BitIn_YesTime_YesWave(WaveTableStateRec* State,
  67.                                                 long SampleCount, largefixedsigned* RawBuffer);
  68. static void                        Wave_Mono_16BitIn_YesTime_YesWave(WaveTableStateRec* State,
  69.                                                 long SampleCount, largefixedsigned* RawBuffer);
  70. static void                        Wave_Stereo_16BitIn_YesTime_YesWave(WaveTableStateRec* State,
  71.                                                 long SampleCount, largefixedsigned* RawBuffer);
  72.  
  73. static void                        Wave_NoOutput(WaveTableStateRec* State,
  74.                                                 long SampleCount, largefixedsigned* RawBuffer);
  75.  
  76.  
  77. /* wave table oscillator template information record */
  78. struct WaveTableTemplateRec
  79.     {
  80.         /* source information for the wave table */
  81.         MultiSampleRec*                WaveTableSourceSelector;
  82.  
  83.         /* sampling rate for final output */
  84.         long                                    FinalOutputSamplingRate;
  85.         /* number of envelope updates per second */
  86.         float                                    EnvelopeTicksPerSecond;
  87.  
  88.         /* values for scaling the frequency of something.  if we were really serious about */
  89.         /* this, we'd traverse all of the oscillators with integral multiples or harmonic */
  90.         /* fractions of the pitch & set their differentials to the same precision as the */
  91.         /* worst oscillator so that they would all stay in sync as time progressed. */
  92.         float                                    FrequencyMultiplier;
  93.         /* this is added after the frequency multiplier is applied */
  94.         float                                    FrequencyAdder;
  95.  
  96.         /* stereo status */
  97.         NumChannelsType                StereoPlayback;
  98.         /* time interpolation flag */
  99.         MyBoolean                            InterpolateThroughTime;
  100.         /* inter-wave interpolation flag */
  101.         MyBoolean                            InterpolateAcrossWaves;
  102.         /* include in final output flag */
  103.         MyBoolean                            IncludeInFinalOutput;
  104.  
  105.         /* envelope templates */
  106.         EnvelopeRec*                    LoudnessEnvelopeTemplate;
  107.         LFOListSpecRec*                LoudnessLFOTemplate;
  108.         EnvelopeRec*                    IndexEnvelopeTemplate;
  109.         LFOListSpecRec*                IndexLFOTemplate;
  110.  
  111.         /* miscellaneous control parameters */
  112.         float                                    StereoBias;
  113.         float                                    TimeDisplacement;
  114.         float                                    OverallOscillatorLoudness;
  115.  
  116.         /* error logging facility */
  117.         ErrorDaemonRec*                ErrorDaemon;
  118.  
  119.         /* link for list control */
  120.         WaveTableTemplateRec*    Next;
  121.     };
  122.  
  123.  
  124. /* wave table oscillator state record */
  125. struct WaveTableStateRec
  126.     {
  127.         /* current sample position into the wave table */
  128.         LongLongRec                        WaveTableSamplePosition; /* 32-bit fixed point */
  129.         /* current increment value for the wave table sample position */
  130.         LongLongRec                        WaveTableSamplePositionDifferential; /* 32-bit fixed point */
  131.  
  132.         /* envelope tick countdown for pre-start time */
  133.         long                                    PreStartCountdown;
  134.  
  135.         /* function for generating a bunch of samples from the wave table */
  136.         void                                    (*WaveTableGenSamples)(WaveTableStateRec* State,
  137.                                                         long SampleCount, largefixedsigned* RawBuffer);
  138.  
  139.         /* number of frames per table */
  140.         long                                    FramesPerTable;
  141.         /* number of tables */
  142.         long                                    NumberOfTables;
  143.         /* raw wave table data array */
  144.         void**                                WaveTableMatrix;
  145.         /* number of bits in the data for the table */
  146.         NumBitsType                        TableNumBits;
  147.  
  148.         /* current index into table of waves.  0 = lowest wave table, NumberOfTables = highest */
  149.         FastFixedType                    WaveTableIndex;
  150.         /* envelope controlling wave table index */
  151.         EvalEnvelopeRec*            WaveTableIndexEnvelope;
  152.         /* LFO generators modifying the output of the index envelope generator */
  153.         LFOGenRec*                        IndexLFOGenerator;
  154.  
  155.         /* NOTE:  either OverallLoudness or Left/RightLoudness are used, but not both */
  156.         /* current overall loudness for oscillator */
  157.         FastFixedType                    OverallLoudness; /* 15-bit fixed point, 0..1 */
  158.         /* left channel loudness */
  159.         FastFixedType                    LeftLoudness; /* 15-bit fixed point, 0..1 */
  160.         /* right channel loudness */
  161.         FastFixedType                    RightLoudness; /* 15-bit fixed point, 0..1 */
  162.         /* panning position for splitting envelope generator into stereo channels */
  163.         /* 0 = left channel, 0.5 = middle, 1 = right channel */
  164.         FastFixedType                    Panning; /* 15-bit fixed point, -1..1 */
  165.         /* envelope that is generating the loudness information */
  166.         EvalEnvelopeRec*            WaveTableLoudnessEnvelope;
  167.         /* LFO generators modifying the output of the loudness envelope generator */
  168.         LFOGenRec*                        LoudnessLFOGenerator;
  169.  
  170.         /* this flag is True if the wave table data was defined at the specified pitch */
  171.         /* (and the wave table array is thus valid) or False if there is no wave table */
  172.         /* at this pitch (and the array is invalid) */
  173.         MyBoolean                            WaveTableWasDefined;
  174.  
  175.         /* this field contains the overall volume scaling for everything so that we */
  176.         /* can treat the envelopes as always going between 0 and 1. */
  177.         float                                    NoteLoudnessScaling;
  178.  
  179.         /* static information for the wave table */
  180.         WaveTableTemplateRec    Template; /* a copy of the data */
  181.  
  182.         /* link for list control */
  183.         WaveTableStateRec*        Next;
  184.     };
  185.  
  186.  
  187.  
  188.  
  189. static WaveTableTemplateRec*        WaveTableTemplateFreeList = NIL;
  190. static WaveTableStateRec*                WaveTableStateFreeList = NIL;
  191.  
  192.  
  193. /* get rid of all cached memory for state or template records */
  194. void                                    FlushWaveTableOscControl(void)
  195.     {
  196.         while (WaveTableTemplateFreeList != NIL)
  197.             {
  198.                 WaveTableTemplateRec*        Temp;
  199.  
  200.                 Temp = WaveTableTemplateFreeList;
  201.                 WaveTableTemplateFreeList = WaveTableTemplateFreeList->Next;
  202.                 ReleasePtr((char*)Temp);
  203.             }
  204.  
  205.         while (WaveTableStateFreeList != NIL)
  206.             {
  207.                 WaveTableStateRec*        Temp;
  208.  
  209.                 Temp = WaveTableStateFreeList;
  210.                 WaveTableStateFreeList = WaveTableStateFreeList->Next;
  211.                 ReleasePtr((char*)Temp);
  212.             }
  213.     }
  214.  
  215.  
  216. #if DEBUG
  217. static void                        CheckValidWaveTableTemplate(WaveTableTemplateRec* Object)
  218.     {
  219.         WaveTableTemplateRec*    Scan;
  220.  
  221.         Scan = WaveTableTemplateFreeList;
  222.         while (Scan != NIL)
  223.             {
  224.                 if (Scan == Object)
  225.                     {
  226.                         PRERR(ForceAbort,"CheckValidWaveTableTemplate:  template is on free list");
  227.                     }
  228.                 Scan = Scan->Next;
  229.             }
  230.     }
  231. #else
  232. #define CheckValidWaveTableTemplate(x) ((void)0)
  233. #endif
  234.  
  235.  
  236. #if DEBUG
  237. static void                        CheckValidWaveTableState(WaveTableStateRec* Object)
  238.     {
  239.         WaveTableStateRec*        Scan;
  240.  
  241.         Scan = WaveTableStateFreeList;
  242.         while (Scan != NIL)
  243.             {
  244.                 if (Scan == Object)
  245.                     {
  246.                         PRERR(ForceAbort,"CheckValidWaveTableState:  state is on free list");
  247.                     }
  248.                 Scan = Scan->Next;
  249.             }
  250.     }
  251. #else
  252. #define CheckValidWaveTableState(x) ((void)0)
  253. #endif
  254.  
  255.  
  256. /* perform one envelope update cycle */
  257. void                                    UpdateWaveTableEnvelopes(WaveTableStateRec* State)
  258.     {
  259.         FastFixedType                Temp;
  260.  
  261.         CheckPtrExistence(State);
  262.         CheckValidWaveTableState(State);
  263.  
  264.         /* this is for the benefit of resampling only -- envelope generators do their */
  265.         /* own pre-origin sequencing */
  266.         if (State->PreStartCountdown > 0)
  267.             {
  268.                 State->PreStartCountdown -= 1;
  269.             }
  270.  
  271.         Temp = (State->NumberOfTables - 1) * LFOGenUpdateCycle(State->IndexLFOGenerator,
  272.             EnvelopeUpdate(State->WaveTableIndexEnvelope));
  273.         if (Temp < 0)
  274.             {
  275.                 Temp = 0;
  276.             }
  277.         else if (Temp > Int2FastFixed(State->NumberOfTables - 1))
  278.             {
  279.                 Temp = Int2FastFixed(State->NumberOfTables - 1);
  280.             }
  281.         State->WaveTableIndex = Temp;
  282.  
  283.         Temp = State->NoteLoudnessScaling * LFOGenUpdateCycle(State->LoudnessLFOGenerator,
  284.             EnvelopeUpdate(State->WaveTableLoudnessEnvelope));
  285.         /* fast fixed has a very narrow range, so overflow can't be permitted: */
  286.         /* 15x15->30 bits, with 2 extra bits; we use one for sign and the other */
  287.         /* to permit the representation of 1 and -1. */
  288.         if (State->Template.StereoPlayback == eSampleMono)
  289.             {
  290.                 if (Temp < - Int2FastFixed(1))
  291.                     {
  292.                         ErrorDaemonReportClamping(State->Template.ErrorDaemon,
  293.                             - FastFixed2Float(Temp));
  294.                         Temp = - Int2FastFixed(1);
  295.                     }
  296.                 else if (Temp > Int2FastFixed(1))
  297.                     {
  298.                         ErrorDaemonReportClamping(State->Template.ErrorDaemon,
  299.                             FastFixed2Float(Temp));
  300.                         Temp = Int2FastFixed(1);
  301.                     }
  302.                 State->OverallLoudness = Temp;
  303.             }
  304.          else
  305.             {
  306.                 FastFixedType                LeftVolumeScaling;
  307.                 FastFixedType                RightVolumeScaling;
  308.                 FastFixedType                MaxVolScaling;
  309.                 FastFixedType                MaxTemp;
  310.  
  311.                 LeftVolumeScaling = FastFixedTimesFastFixedToFastFixed(Double2FastFixed(0.5),
  312.                     Int2FastFixed(1) - State->Panning);
  313.                 RightVolumeScaling = FastFixedTimesFastFixedToFastFixed(Double2FastFixed(0.5),
  314.                     Int2FastFixed(1) + State->Panning);
  315.  
  316.                 if (((LeftVolumeScaling >= 0) ? LeftVolumeScaling : (- LeftVolumeScaling))
  317.                     > ((RightVolumeScaling >= 0) ? RightVolumeScaling : (- RightVolumeScaling)))
  318.                     {
  319.                         MaxVolScaling = ((LeftVolumeScaling >= 0)
  320.                             ? LeftVolumeScaling : (- LeftVolumeScaling));
  321.                     }
  322.                  else
  323.                     {
  324.                         MaxVolScaling = ((RightVolumeScaling >= 0)
  325.                             ? RightVolumeScaling : (- RightVolumeScaling));
  326.                     }
  327.                 MaxTemp = Double2FastFixed((float)1 / FastFixed2Float(MaxVolScaling));
  328.                 if (Temp < - MaxTemp)
  329.                     {
  330.                         ErrorDaemonReportClamping(State->Template.ErrorDaemon,
  331.                             - FastFixed2Float(Temp) / FastFixed2Float(MaxTemp));
  332.                         Temp = - MaxTemp;
  333.                     }
  334.                 else if (Temp > MaxTemp)
  335.                     {
  336.                         ErrorDaemonReportClamping(State->Template.ErrorDaemon,
  337.                             FastFixed2Float(Temp) / FastFixed2Float(MaxTemp));
  338.                         Temp = MaxTemp;
  339.                     }
  340.  
  341.                 State->LeftLoudness = FastFixedTimesFastFixedToFastFixed(
  342.                     LeftVolumeScaling,Temp);
  343.                 State->RightLoudness = FastFixedTimesFastFixedToFastFixed(
  344.                     RightVolumeScaling,Temp);
  345.             }
  346.     }
  347.  
  348.  
  349. /* dispose of the wave table state record */
  350. void                                    DisposeWaveTableState(WaveTableStateRec* State)
  351.     {
  352.         CheckPtrExistence(State);
  353.         CheckValidWaveTableState(State);
  354.  
  355.         DisposeEnvelopeStateRecord(State->WaveTableIndexEnvelope);
  356.         DisposeLFOGenerator(State->IndexLFOGenerator);
  357.         DisposeEnvelopeStateRecord(State->WaveTableLoudnessEnvelope);
  358.         DisposeLFOGenerator(State->LoudnessLFOGenerator);
  359.  
  360.         State->Next = WaveTableStateFreeList;
  361.         WaveTableStateFreeList = State;
  362.     }
  363.  
  364.  
  365. /* dispose of the wave table information template */
  366. void                                    DisposeWaveTableTemplate(WaveTableTemplateRec* Template)
  367.     {
  368.         CheckPtrExistence(Template);
  369.         CheckValidWaveTableTemplate(Template);
  370.  
  371.         DisposeMultisample(Template->WaveTableSourceSelector);
  372.  
  373.         Template->Next = WaveTableTemplateFreeList;
  374.         WaveTableTemplateFreeList = Template;
  375.     }
  376.  
  377.  
  378. /* create a new wave table template */
  379. WaveTableTemplateRec*    NewWaveTableTemplate(struct OscillatorRec* Oscillator,
  380.                                                 float EnvelopeTicksPerSecond, long SamplingRate,
  381.                                                 MyBoolean Stereo, MyBoolean TimeInterp, MyBoolean WaveInterp,
  382.                                                 ErrorDaemonRec* ErrorDaemon)
  383.     {
  384.         WaveTableTemplateRec*    Template;
  385.  
  386.         CheckPtrExistence(ErrorDaemon);
  387.         CheckPtrExistence(Oscillator);
  388.         ERROR(OscillatorGetWhatKindItIs(Oscillator) != eOscillatorWaveTable,PRERR(ForceAbort,
  389.             "NewWaveTableTemplate:  oscillator is not a wave table"));
  390.  
  391.         if (WaveTableTemplateFreeList != NIL)
  392.             {
  393.                 Template = WaveTableTemplateFreeList;
  394.                 WaveTableTemplateFreeList = WaveTableTemplateFreeList->Next;
  395.             }
  396.          else
  397.             {
  398.                 Template = (WaveTableTemplateRec*)AllocPtrCanFail(sizeof(WaveTableTemplateRec),
  399.                     "WaveTableTemplateRec");
  400.                 if (Template == NIL)
  401.                     {
  402.                         return NIL;
  403.                     }
  404.             }
  405.         EXECUTE(Template->Next = (WaveTableTemplateRec*)0x81818181;)
  406.  
  407.         Template->WaveTableSourceSelector = NewMultisampleWaveTable(
  408.             OscillatorGetSampleIntervalList(Oscillator));
  409.         if (Template->WaveTableSourceSelector == NIL)
  410.             {
  411.              FailurePoint1:
  412.                 Template->Next = WaveTableTemplateFreeList;
  413.                 WaveTableTemplateFreeList = Template;
  414.                 return NIL;
  415.             }
  416.  
  417.         Template->FinalOutputSamplingRate = SamplingRate;
  418.         Template->EnvelopeTicksPerSecond = EnvelopeTicksPerSecond;
  419.         Template->OverallOscillatorLoudness = OscillatorGetOutputLoudness(Oscillator);
  420.  
  421.         /* it might be better to handle divisor and multiplier separately -- we would */
  422.         /* want to do that if we were trying to guarantee that all harmonic */
  423.         /* oscillators ran in lock-step */
  424.         Template->FrequencyMultiplier = OscillatorGetFrequencyMultiplier(Oscillator)
  425.             / OscillatorGetFrequencyDivisor(Oscillator);
  426.         Template->FrequencyAdder = OscillatorGetFrequencyAdder(Oscillator);
  427.  
  428.         Template->StereoBias = OscillatorGetStereoBias(Oscillator);
  429.         Template->TimeDisplacement = OscillatorGetTimeDisplacement(Oscillator);
  430.  
  431.         if (Stereo)
  432.             {
  433.                 Template->StereoPlayback = eSampleStereo;
  434.             }
  435.          else
  436.             {
  437.                 Template->StereoPlayback = eSampleMono;
  438.             }
  439.         Template->InterpolateThroughTime = TimeInterp;
  440.         Template->InterpolateAcrossWaves = WaveInterp;
  441.         Template->IncludeInFinalOutput = IncludeOscillatorInFinalOutput(Oscillator);
  442.  
  443.         /* these are just references */
  444.         Template->LoudnessEnvelopeTemplate = OscillatorGetLoudnessEnvelope(Oscillator);
  445.         Template->LoudnessLFOTemplate = OscillatorGetLoudnessLFOList(Oscillator);
  446.         Template->IndexEnvelopeTemplate = OscillatorGetExcitationEnvelope(Oscillator);
  447.         Template->IndexLFOTemplate = OscillatorGetExcitationLFOList(Oscillator);
  448.  
  449.         /* another reference */
  450.         Template->ErrorDaemon = ErrorDaemon;
  451.  
  452.         return Template;
  453.     }
  454.  
  455.  
  456. /* create a new wave table state object. */
  457. WaveTableStateRec*        NewWaveTableState(WaveTableTemplateRec* Template,
  458.                                                 float FreqForMultisampling, float Accent1, float Accent2,
  459.                                                 float Accent3, float Accent4, float Loudness, float HurryUp,
  460.                                                 long* PreOriginTimeOut, float StereoPosition,
  461.                                                 float InitialFrequency)
  462.     {
  463.         WaveTableStateRec*    State;
  464.         long                                MaxPreOrigin;
  465.         long                                OnePreOrigin;
  466.  
  467.         CheckPtrExistence(Template);
  468.         CheckValidWaveTableTemplate(Template);
  469.  
  470.         if (WaveTableStateFreeList != NIL)
  471.             {
  472.                 State = WaveTableStateFreeList;
  473.                 WaveTableStateFreeList = WaveTableStateFreeList->Next;
  474.             }
  475.          else
  476.             {
  477.                 State = (WaveTableStateRec*)AllocPtrCanFail(sizeof(WaveTableStateRec),
  478.                     "WaveTableStateRec");
  479.                 if (State == NIL)
  480.                     {
  481.                      FailurePoint1:
  482.                         return NIL;
  483.                     }
  484.             }
  485.         EXECUTE(State->Next = (WaveTableStateRec*)0x81818181;)
  486.  
  487.         State->Template = *Template;
  488.  
  489.         MaxPreOrigin = 0;
  490.  
  491.         Double2LongLong(0,State->WaveTableSamplePosition);
  492.         /* State->WaveTableSamplePositionDifferential specified in separate call */
  493.  
  494.         State->NoteLoudnessScaling = Loudness * Template->OverallOscillatorLoudness;
  495.  
  496.         State->WaveTableWasDefined = GetMultisampleReferenceWaveTable(
  497.             Template->WaveTableSourceSelector,FreqForMultisampling,&(State->WaveTableMatrix),
  498.             &(State->FramesPerTable),&(State->NumberOfTables),&(State->TableNumBits));
  499.  
  500.         /* Template->IncludeInFinalOutput: */
  501.         /*   the routine pointer is not used for modulation */
  502.         /* State->WaveTableWasDefined: */
  503.         /*   if there is no wave table defined for the current pitch, then we don't */
  504.         /*   bother generating any data */
  505.         /* State->FramesPerTable > 0: */
  506.         /*   if the wave table is empty, then we don't do any work (and we must not, */
  507.         /*   since array accesses would cause a crash) */
  508.         if (Template->IncludeInFinalOutput && State->WaveTableWasDefined
  509.             && (State->FramesPerTable > 0))
  510.             {
  511.                 if (Template->StereoPlayback == eSampleStereo)
  512.                     {
  513.                         if (State->TableNumBits == eSample16bit)
  514.                             {
  515.                                 if (Template->InterpolateAcrossWaves)
  516.                                     {
  517.                                         State->WaveTableGenSamples = &Wave_Stereo_16BitIn_YesTime_YesWave;
  518.                                     }
  519.                                 else if (Template->InterpolateThroughTime)
  520.                                     {
  521.                                         State->WaveTableGenSamples = &Wave_Stereo_16BitIn_YesTime_NoWave;
  522.                                     }
  523.                                 else
  524.                                     {
  525.                                         State->WaveTableGenSamples = &Wave_Stereo_16BitIn_NoTime_NoWave;
  526.                                     }
  527.                             }
  528.                          else
  529.                             {
  530.                                 if (Template->InterpolateAcrossWaves)
  531.                                     {
  532.                                         State->WaveTableGenSamples = &Wave_Stereo_8BitIn_YesTime_YesWave;
  533.                                     }
  534.                                 else if (Template->InterpolateThroughTime)
  535.                                     {
  536.                                         State->WaveTableGenSamples = &Wave_Stereo_8BitIn_YesTime_NoWave;
  537.                                     }
  538.                                 else
  539.                                     {
  540.                                         State->WaveTableGenSamples = &Wave_Stereo_8BitIn_NoTime_NoWave;
  541.                                     }
  542.                             }
  543.                     }
  544.                  else
  545.                     {
  546.                         if (State->TableNumBits == eSample16bit)
  547.                             {
  548.                                 if (Template->InterpolateAcrossWaves)
  549.                                     {
  550.                                         State->WaveTableGenSamples = &Wave_Mono_16BitIn_YesTime_YesWave;
  551.                                     }
  552.                                 else if (Template->InterpolateThroughTime)
  553.                                     {
  554.                                         State->WaveTableGenSamples = &Wave_Mono_16BitIn_YesTime_NoWave;
  555.                                     }
  556.                                 else
  557.                                     {
  558.                                         State->WaveTableGenSamples = &Wave_Mono_16BitIn_NoTime_NoWave;
  559.                                     }
  560.                             }
  561.                          else
  562.                             {
  563.                                 if (Template->InterpolateAcrossWaves)
  564.                                     {
  565.                                         State->WaveTableGenSamples = &Wave_Mono_8BitIn_YesTime_YesWave;
  566.                                     }
  567.                                 else if (Template->InterpolateThroughTime)
  568.                                     {
  569.                                         State->WaveTableGenSamples = &Wave_Mono_8BitIn_YesTime_NoWave;
  570.                                     }
  571.                                 else
  572.                                     {
  573.                                         State->WaveTableGenSamples = &Wave_Mono_8BitIn_NoTime_NoWave;
  574.                                     }
  575.                             }
  576.                     }
  577.             }
  578.          else
  579.             {
  580.                 State->WaveTableGenSamples = &Wave_NoOutput;
  581.             }
  582.  
  583.         State->PreStartCountdown = Template->TimeDisplacement
  584.             * Template->EnvelopeTicksPerSecond + 0.5;
  585.         if (- State->PreStartCountdown > MaxPreOrigin)
  586.             {
  587.                 MaxPreOrigin = - State->PreStartCountdown;
  588.             }
  589.  
  590.         /* State->WaveTableIndex determined by envelope update */
  591.         State->WaveTableIndexEnvelope = NewEnvelopeStateRecord(
  592.             Template->IndexEnvelopeTemplate,Accent1,Accent2,Accent3,Accent4,InitialFrequency,
  593.             1,HurryUp,Template->EnvelopeTicksPerSecond,&OnePreOrigin);
  594.         if (State->WaveTableIndexEnvelope == NIL)
  595.             {
  596.              FailurePoint2:
  597.                 State->Next = WaveTableStateFreeList;
  598.                 WaveTableStateFreeList = State;
  599.                 goto FailurePoint1;
  600.             }
  601.         if (OnePreOrigin > MaxPreOrigin)
  602.             {
  603.                 MaxPreOrigin = OnePreOrigin;
  604.             }
  605.         State->IndexLFOGenerator = NewLFOGenerator(Template->IndexLFOTemplate,
  606.             &OnePreOrigin,Accent1,Accent2,Accent3,Accent4,InitialFrequency,HurryUp,
  607.             Template->EnvelopeTicksPerSecond,1,1,eLFOArithDefault,FreqForMultisampling);
  608.         if (State->IndexLFOGenerator == NIL)
  609.             {
  610.              FailurePoint3:
  611.                 DisposeEnvelopeStateRecord(State->WaveTableIndexEnvelope);
  612.                 goto FailurePoint2;
  613.             }
  614.         if (OnePreOrigin > MaxPreOrigin)
  615.             {
  616.                 MaxPreOrigin = OnePreOrigin;
  617.             }
  618.  
  619.         /* State->OverallLoudness, State->LeftLoudness, State->RightLoudness */
  620.         /* are determined by the envelope update */
  621.         StereoPosition += Template->StereoBias;
  622.         if (StereoPosition < -1)
  623.             {
  624.                 StereoPosition = -1;
  625.             }
  626.         else if (StereoPosition > 1)
  627.             {
  628.                 StereoPosition = 1;
  629.             }
  630.         State->Panning = Double2FastFixed(StereoPosition);
  631.         State->WaveTableLoudnessEnvelope = NewEnvelopeStateRecord(
  632.             Template->LoudnessEnvelopeTemplate,Accent1,Accent2,Accent3,Accent4,
  633.             InitialFrequency,1,HurryUp,Template->EnvelopeTicksPerSecond,&OnePreOrigin);
  634.         if (State->WaveTableLoudnessEnvelope == NIL)
  635.             {
  636.              FailurePoint4:
  637.                 DisposeLFOGenerator(State->IndexLFOGenerator);
  638.                 goto FailurePoint3;
  639.             }
  640.         if (OnePreOrigin > MaxPreOrigin)
  641.             {
  642.                 MaxPreOrigin = OnePreOrigin;
  643.             }
  644.         State->LoudnessLFOGenerator = NewLFOGenerator(Template->LoudnessLFOTemplate,
  645.             &OnePreOrigin,Accent1,Accent2,Accent3,Accent4,InitialFrequency,HurryUp,
  646.             Template->EnvelopeTicksPerSecond,1,1,eLFOArithDefault,FreqForMultisampling);
  647.         if (State->LoudnessLFOGenerator == NIL)
  648.             {
  649.              FailurePoint5:
  650.                 DisposeEnvelopeStateRecord(State->WaveTableLoudnessEnvelope);
  651.                 goto FailurePoint4;
  652.             }
  653.         if (OnePreOrigin > MaxPreOrigin)
  654.             {
  655.                 MaxPreOrigin = OnePreOrigin;
  656.             }
  657.  
  658.         *PreOriginTimeOut = MaxPreOrigin;
  659.         return State;
  660.     }
  661.  
  662.  
  663. /* fix up pre-origin time for the wave table state object */
  664. void                                    FixUpWaveTableStatePreOrigin(WaveTableStateRec* State,
  665.                                                 long ActualPreOrigin)
  666.     {
  667.         CheckPtrExistence(State);
  668.         CheckValidWaveTableState(State);
  669.  
  670.         EnvelopeStateFixUpInitialDelay(State->WaveTableIndexEnvelope,ActualPreOrigin);
  671.         EnvelopeStateFixUpInitialDelay(State->WaveTableLoudnessEnvelope,ActualPreOrigin);
  672.         LFOGeneratorFixEnvelopeOrigins(State->IndexLFOGenerator,ActualPreOrigin);
  673.         LFOGeneratorFixEnvelopeOrigins(State->LoudnessLFOGenerator,ActualPreOrigin);
  674.  
  675.         State->PreStartCountdown += ActualPreOrigin;
  676.     }
  677.  
  678.  
  679. /* set a new frequency for a wave table state object.  used for portamento */
  680. /* and modulation of frequency (vibrato) */
  681. void                                    WaveTableStateNewFrequency(WaveTableStateRec* State,
  682.                                                 float NewFrequencyHertz)
  683.     {
  684.         CheckPtrExistence(State);
  685.         CheckValidWaveTableState(State);
  686.  
  687.         Double2LongLong((NewFrequencyHertz * State->Template.FrequencyMultiplier
  688.             + State->Template.FrequencyAdder) / State->Template.FinalOutputSamplingRate
  689.             * State->FramesPerTable,State->WaveTableSamplePositionDifferential);
  690.     }
  691.  
  692.  
  693. /* send a key-up signal to one of the oscillators */
  694. void                                    WaveTableKeyUpSustain1(WaveTableStateRec* State)
  695.     {
  696.         CheckPtrExistence(State);
  697.         CheckValidWaveTableState(State);
  698.         LFOGeneratorKeyUpSustain1(State->IndexLFOGenerator);
  699.         LFOGeneratorKeyUpSustain1(State->LoudnessLFOGenerator);
  700.         EnvelopeKeyUpSustain1(State->WaveTableIndexEnvelope);
  701.         EnvelopeKeyUpSustain1(State->WaveTableLoudnessEnvelope);
  702.     }
  703.  
  704.  
  705. /* send a key-up signal to one of the oscillators */
  706. void                                    WaveTableKeyUpSustain2(WaveTableStateRec* State)
  707.     {
  708.         CheckPtrExistence(State);
  709.         CheckValidWaveTableState(State);
  710.         LFOGeneratorKeyUpSustain2(State->IndexLFOGenerator);
  711.         LFOGeneratorKeyUpSustain2(State->LoudnessLFOGenerator);
  712.         EnvelopeKeyUpSustain2(State->WaveTableIndexEnvelope);
  713.         EnvelopeKeyUpSustain2(State->WaveTableLoudnessEnvelope);
  714.     }
  715.  
  716.  
  717. /* send a key-up signal to one of the oscillators */
  718. void                                    WaveTableKeyUpSustain3(WaveTableStateRec* State)
  719.     {
  720.         CheckPtrExistence(State);
  721.         CheckValidWaveTableState(State);
  722.         LFOGeneratorKeyUpSustain3(State->IndexLFOGenerator);
  723.         LFOGeneratorKeyUpSustain3(State->LoudnessLFOGenerator);
  724.         EnvelopeKeyUpSustain3(State->WaveTableIndexEnvelope);
  725.         EnvelopeKeyUpSustain3(State->WaveTableLoudnessEnvelope);
  726.     }
  727.  
  728.  
  729. /* restart a wave table oscillator.  this is used for tie continuations */
  730. void                                    RestartWaveTableState(WaveTableStateRec* State,
  731.                                                 float NewFreqMultisampling, float NewAccent1, float NewAccent2,
  732.                                                 float NewAccent3, float NewAccent4, float NewLoudness,
  733.                                                 float NewHurryUp, MyBoolean RetriggerEnvelopes,
  734.                                                 float NewStereoPosition, float NewInitialFrequency)
  735.     {
  736.         CheckPtrExistence(State);
  737.         CheckValidWaveTableState(State);
  738.  
  739.         NewStereoPosition += State->Template.StereoBias;
  740.         if (NewStereoPosition < -1)
  741.             {
  742.                 NewStereoPosition = -1;
  743.             }
  744.         else if (NewStereoPosition > 1)
  745.             {
  746.                 NewStereoPosition = 1;
  747.             }
  748.         State->Panning = Double2FastFixed(NewStereoPosition);
  749.  
  750.         State->NoteLoudnessScaling = NewLoudness * State->Template.OverallOscillatorLoudness;
  751.  
  752.         EnvelopeRetriggerFromOrigin(State->WaveTableIndexEnvelope,NewAccent1,NewAccent2,
  753.             NewAccent3,NewAccent4,NewInitialFrequency,1,NewHurryUp,
  754.             State->Template.EnvelopeTicksPerSecond,RetriggerEnvelopes);
  755.         EnvelopeRetriggerFromOrigin(State->WaveTableLoudnessEnvelope,NewAccent1,
  756.             NewAccent2,NewAccent3,NewAccent4,NewInitialFrequency,1,NewHurryUp,
  757.             State->Template.EnvelopeTicksPerSecond,RetriggerEnvelopes);
  758.         LFOGeneratorRetriggerFromOrigin(State->IndexLFOGenerator,NewAccent1,NewAccent2,
  759.             NewAccent3,NewAccent4,NewInitialFrequency,NewHurryUp,
  760.             State->Template.EnvelopeTicksPerSecond,1,1,RetriggerEnvelopes);
  761.         LFOGeneratorRetriggerFromOrigin(State->LoudnessLFOGenerator,NewAccent1,
  762.             NewAccent2,NewAccent3,NewAccent4,NewInitialFrequency,NewHurryUp,
  763.             State->Template.EnvelopeTicksPerSecond,1,1,RetriggerEnvelopes);
  764.     }
  765.  
  766.  
  767. /* generate one sequence of samples */
  768. void                                    WaveTableGenSamples(WaveTableStateRec* State,
  769.                                                 long SampleCount, largefixedsigned* RawBuffer)
  770.     {
  771.         CheckPtrExistence(State);
  772.         CheckValidWaveTableState(State);
  773.  
  774.         if (State->PreStartCountdown <= 0)
  775.             {
  776.                 (*State->WaveTableGenSamples)(State,SampleCount,RawBuffer);
  777.             }
  778.     }
  779.  
  780.  
  781. /* find out if the wave table oscillator has finished */
  782. MyBoolean                            WaveTableIsItFinished(WaveTableStateRec* State)
  783.     {
  784.         CheckPtrExistence(State);
  785.         CheckValidWaveTableState(State);
  786.  
  787.         /* we are finished when one of the following conditions is met: */
  788.         /*  - output volume is zero AND loudness envelope is finished */
  789.         /*  - we are not generating any signal */
  790.         if (!State->WaveTableWasDefined)
  791.             {
  792.                 return True;
  793.             }
  794.         if (IsEnvelopeAtEnd(State->WaveTableLoudnessEnvelope))
  795.             {
  796.                 if (State->Template.StereoPlayback == eSampleStereo)
  797.                     {
  798.                         if ((State->LeftLoudness == 0) && (State->RightLoudness == 0))
  799.                             {
  800.                                 return True;
  801.                             }
  802.                     }
  803.                  else
  804.                     {
  805.                         if (State->OverallLoudness == 0)
  806.                             {
  807.                                 return True;
  808.                             }
  809.                     }
  810.             }
  811.         return IsEnvelopeAtEnd(State->WaveTableLoudnessEnvelope);
  812.     }
  813.  
  814.  
  815. /* generate a single sample (called for modulation chains) */
  816. /* OutputPlace should have 1 entry for mono output or 2 entries for stereo output */
  817. float                                    WaveTableGenOneSample(WaveTableStateRec* State,
  818.                                                 ModulationTypes* PhaseGenModulateHow,
  819.                                                 float* PhaseGenModulationScaling,
  820.                                                 float* PhaseGenModulationOrigin,
  821.                                                 float* PhaseGenOldValues,
  822.                                                 long* PhaseGenIndirectionTable,
  823.                                                 long NumberOfPhaseGenModulators,
  824.                                                 ModulationTypes* OutputModulateHow,
  825.                                                 float* OutputModulationScaling,
  826.                                                 float* OutputModulationOrigin,
  827.                                                 float* OutputOldValues,
  828.                                                 long* OutputIndirectionTable,
  829.                                                 long NumberOfOutputModulators,
  830.                                                 largefixedsigned* OutputPlace)
  831.     {
  832.         float                                ReturnValue;
  833.         LongLongRec                    FrameIndex;
  834.         signed long                    Final; /* both 16-bit int & largefixedsigned */
  835.  
  836.         CheckPtrExistence(State);
  837.         CheckValidWaveTableState(State);
  838.  
  839.         /* should we bother doing any work? */
  840.         if (!State->WaveTableWasDefined || (State->FramesPerTable == 0)
  841.             || (State->PreStartCountdown > 0))
  842.             {
  843.                 /* modulate against zero */
  844.                 if (State->Template.StereoPlayback == eSampleStereo)
  845.                     {
  846.                         OutputPlace[0] += double2largefixed(ApplyModulation(0,OutputModulateHow,
  847.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  848.                             OutputIndirectionTable,NumberOfOutputModulators));
  849.                         OutputPlace[1] += double2largefixed(ApplyModulation(0,OutputModulateHow,
  850.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  851.                             OutputIndirectionTable,NumberOfOutputModulators));
  852.                     }
  853.                  else
  854.                     {
  855.                         OutputPlace[0] += double2largefixed(ApplyModulation(0,OutputModulateHow,
  856.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  857.                             OutputIndirectionTable,NumberOfOutputModulators));
  858.                     }
  859.                 return 0;
  860.             }
  861.  
  862.         /* generate the sample position */
  863.         /* we want the modulation to be the same no matter how large the wavetable */
  864.         /* is, so we need to normalize the wave table index. */
  865.         Double2LongLong(ApplyModulation(LongLong2Double(State->WaveTableSamplePosition)
  866.             / State->FramesPerTable,PhaseGenModulateHow,PhaseGenModulationScaling,
  867.             PhaseGenModulationOrigin,PhaseGenOldValues,PhaseGenIndirectionTable,
  868.             NumberOfPhaseGenModulators) * State->FramesPerTable,FrameIndex);
  869.  
  870.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  871.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  872.             "WaveTableGenOneSample:  wave index out of range"));
  873.  
  874.         /* big selector of the thing to do */
  875.         if (State->Template.InterpolateAcrossWaves)
  876.             {
  877.                 /* wave & time interp */
  878.                 /* do the stuff for one wave table */
  879.                 if (State->TableNumBits == eSample16bit)
  880.                     {
  881.                         /* 16-bit sampling, wave interp, time interp */
  882.  
  883.                         signed short*                WaveData0;
  884.                         signed short*                WaveData1;
  885.                         FastFixedType                Wave0Weight;
  886.  
  887.                         FastFixedType                LeftWeight;
  888.                         long                                ArraySubscript;
  889.                         signed long                    Left0Value;
  890.                         signed long                    Right0Value;
  891.                         signed long                    Left1Value;
  892.                         signed long                    Right1Value;
  893.                         FastFixedType                Wave0Temp;
  894.  
  895.                         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  896.                             {
  897.                                 /* this is done in case the wave table index is at the maximum, */
  898.                                 /* in which case there is no table+1 to interpolate with. */
  899.                                 goto W16BitTimeNoWave;
  900.                             }
  901.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  902.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  903.                             State->WaveTableIndex)]));
  904.                         WaveData0 = (signed short*)(State->WaveTableMatrix[
  905.                             FastFixed2Int(State->WaveTableIndex)]);
  906.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  907.                             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  908.                             State->WaveTableIndex) + 1]));
  909.                         WaveData1 = (signed short*)(State->WaveTableMatrix[
  910.                             FastFixed2Int(State->WaveTableIndex) + 1]);
  911.                         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  912.  
  913.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  914.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  915.                         /* L+F(R-L) -- applied twice */
  916.                         Left0Value = WaveData0[ArraySubscript];
  917.                         Right0Value = WaveData0[ArraySubscript + 1];
  918.                         Left1Value = WaveData1[ArraySubscript];
  919.                         Right1Value = WaveData1[ArraySubscript + 1];
  920.                         Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  921.                         Final = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  922.                             * (Right1Value - Left1Value)) >> 15)) - Wave0Temp) >> 15);
  923.                     }
  924.                  else
  925.                     {
  926.                         /* 8-bit sampling, wave interp, time interp */
  927.  
  928.                         signed char*                WaveData0;
  929.                         signed char*                WaveData1;
  930.                         FastFixedType                Wave0Weight;
  931.  
  932.                         FastFixedType                LeftWeight;
  933.                         long                                ArraySubscript;
  934.                         signed long                    Left0Value;
  935.                         signed long                    Right0Value;
  936.                         signed long                    Left1Value;
  937.                         signed long                    Right1Value;
  938.                         FastFixedType                Wave0Temp;
  939.  
  940.                         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  941.                             {
  942.                                 /* this is done in case the wave table index is at the maximum, */
  943.                                 /* in which case there is no table+1 to interpolate with. */
  944.                                 goto W8BitTimeNoWave;
  945.                             }
  946.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  947.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  948.                             State->WaveTableIndex)]));
  949.                         WaveData0 = (signed char*)(State->WaveTableMatrix[
  950.                             FastFixed2Int(State->WaveTableIndex)]);
  951.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  952.                             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  953.                             State->WaveTableIndex) + 1]));
  954.                         WaveData1 = (signed char*)(State->WaveTableMatrix[
  955.                             FastFixed2Int(State->WaveTableIndex) + 1]);
  956.                         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  957.  
  958.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  959.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  960.                         /* L+F(R-L) -- applied twice */
  961.                         Left0Value = ((signed long)WaveData0[ArraySubscript]) << 8; /* convert to 16-bit */
  962.                         Right0Value = ((signed long)WaveData0[ArraySubscript + 1]) << 8; /* to 16-bit */
  963.                         Left1Value = ((signed long)WaveData1[ArraySubscript]) << 8; /* convert to 16-bit */
  964.                         Right1Value = ((signed long)WaveData1[ArraySubscript + 1]) << 8; /* to 16-bit */
  965.                         Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  966.                         Final = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  967.                             * (Right1Value - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  968.                     }
  969.             }
  970.         else if (State->Template.InterpolateThroughTime)
  971.             {
  972.                 /* time interp */
  973.                 /* do the stuff for one wave table */
  974.                 if (State->TableNumBits == eSample16bit)
  975.                     {
  976.                         /* 16-bit sampling, time interp */
  977.  
  978.                         signed short*                WaveData;
  979.  
  980.                         FastFixedType                LeftWeight;
  981.                         long                                ArraySubscript;
  982.                         signed long                    LeftValue;
  983.                         signed long                    RightValue;
  984.  
  985.                      W16BitTimeNoWave:
  986.  
  987.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  988.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  989.                             State->WaveTableIndex)]));
  990.                         WaveData = (signed short*)(State->WaveTableMatrix[
  991.                             FastFixed2Int(State->WaveTableIndex)]);
  992.  
  993.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  994.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  995.                         /* L+F(R-L) */
  996.                         LeftValue = WaveData[ArraySubscript];
  997.                         RightValue = WaveData[ArraySubscript + 1];
  998.                         Final = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  999.                     }
  1000.                  else
  1001.                     {
  1002.                         /* 8-bit sampling, time interp */
  1003.  
  1004.                         signed char*                WaveData;
  1005.  
  1006.                         FastFixedType                LeftWeight;
  1007.                         long                                ArraySubscript;
  1008.                         signed long                    LeftValue;
  1009.                         signed long                    RightValue;
  1010.  
  1011.                      W8BitTimeNoWave:
  1012.  
  1013.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1014.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1015.                             State->WaveTableIndex)]));
  1016.                         WaveData = (signed char*)(State->WaveTableMatrix[
  1017.                             FastFixed2Int(State->WaveTableIndex)]);
  1018.  
  1019.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  1020.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  1021.                         /* L+F(R-L) */
  1022.                         LeftValue = ((signed long)WaveData[ArraySubscript]) << 8; /* convert to 16-bit */
  1023.                         RightValue = ((signed long)WaveData[ArraySubscript + 1]) << 8; /* to 16-bit */
  1024.                         Final = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  1025.                     }
  1026.             }
  1027.         else
  1028.             {
  1029.                 /* no interp */
  1030.                 /* do the stuff for one wave table */
  1031.                 if (State->TableNumBits == eSample16bit)
  1032.                     {
  1033.                         /* 16-bit sampling */
  1034.  
  1035.                         signed short*                WaveData;
  1036.  
  1037.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1038.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1039.                             State->WaveTableIndex)]));
  1040.                         WaveData = (signed short*)(State->WaveTableMatrix[
  1041.                             FastFixed2Int(State->WaveTableIndex)]);
  1042.  
  1043.                         Final = WaveData[LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1)];
  1044.                     }
  1045.                  else
  1046.                     {
  1047.                         /* 8-bit sampling */
  1048.  
  1049.                         signed char*                WaveData;
  1050.  
  1051.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1052.                             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1053.                             State->WaveTableIndex)]));
  1054.                         WaveData = (signed char*)(State->WaveTableMatrix[
  1055.                             FastFixed2Int(State->WaveTableIndex)]);
  1056.  
  1057.                         Final = WaveData[LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1)];
  1058.                     }
  1059.             }
  1060.  
  1061.         if (State->Template.StereoPlayback == eSampleStereo)
  1062.             {
  1063.                 ReturnValue = ((float)Final / MAX16BIT) * ((FastFixed2Float(
  1064.                     State->LeftLoudness) + FastFixed2Float(State->RightLoudness)) / 2);
  1065.                 if (State->Template.IncludeInFinalOutput)
  1066.                     {
  1067.                         OutputPlace[0] += double2largefixed(ApplyModulation(largefixed2single(
  1068.                             FastFixedTimes16BitTo24Bit((signed long)(State->LeftLoudness
  1069.                             * State->NoteLoudnessScaling),Final)),OutputModulateHow,
  1070.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  1071.                             OutputIndirectionTable,NumberOfOutputModulators));;
  1072.                         OutputPlace[1] += double2largefixed(ApplyModulation(largefixed2single(
  1073.                             FastFixedTimes16BitTo24Bit((signed long)(State->RightLoudness
  1074.                             * State->NoteLoudnessScaling),Final)),OutputModulateHow,
  1075.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  1076.                             OutputIndirectionTable,NumberOfOutputModulators));
  1077.                     }
  1078.             }
  1079.          else
  1080.             {
  1081.                 ReturnValue = ((float)Final / MAX16BIT)
  1082.                     * FastFixed2Float(State->OverallLoudness);
  1083.                 if (State->Template.IncludeInFinalOutput)
  1084.                     {
  1085.                         OutputPlace[0] += double2largefixed(ApplyModulation(largefixed2single(
  1086.                             FastFixedTimes16BitTo24Bit((signed long)(State->OverallLoudness
  1087.                             * State->NoteLoudnessScaling),Final)),OutputModulateHow,
  1088.                             OutputModulationScaling,OutputModulationOrigin,OutputOldValues,
  1089.                             OutputIndirectionTable,NumberOfOutputModulators));
  1090.                     }
  1091.             }
  1092.  
  1093.         LongLongAdd(&State->WaveTableSamplePosition,
  1094.             &(State->WaveTableSamplePositionDifferential));
  1095.         LongLongMaskHighHalf(State->WaveTableSamplePosition,State->FramesPerTable - 1);
  1096.  
  1097.         return ReturnValue;
  1098.     }
  1099.  
  1100.  
  1101. static void                        Wave_Mono_8BitIn_NoTime_NoWave(WaveTableStateRec* State,
  1102.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1103.     {
  1104.         LongLongRec                    LocalWaveTableSamplePosition;
  1105.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1106.         unsigned long                LocalSamplePositionMask;
  1107.         signed char*                WaveData;
  1108.         FastFixedType                LocalOverallLoudness;
  1109.  
  1110.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1111.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1112.             "Wave_Mono_8BitIn_NoTime_NoWave:  wave index out of range"));
  1113.         LocalOverallLoudness = State->OverallLoudness;
  1114.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1115.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1116.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1117.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1118.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1119.             State->WaveTableIndex)]));
  1120.         WaveData = (signed char*)(State->WaveTableMatrix[
  1121.             FastFixed2Int(State->WaveTableIndex)]);
  1122.  
  1123.         while (SampleCount > 0)
  1124.             {
  1125.                 *(RawBuffer++) += FastFixedTimes8BitTo24Bit(LocalOverallLoudness,
  1126.                     WaveData[LongLongHighHalf(LocalWaveTableSamplePosition)
  1127.                     & LocalSamplePositionMask]);
  1128.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1129.                     &LocalWaveTableSamplePositionDifferential);
  1130.                 SampleCount -= 1;
  1131.             }
  1132.  
  1133.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1134.     }
  1135.  
  1136.  
  1137. static void                        Wave_Stereo_8BitIn_NoTime_NoWave(WaveTableStateRec* State,
  1138.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1139.     {
  1140.         LongLongRec                    LocalWaveTableSamplePosition;
  1141.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1142.         unsigned long                LocalSamplePositionMask;
  1143.         signed char*                WaveData;
  1144.         FastFixedType                LocalLeftLoudness;
  1145.         FastFixedType                LocalRightLoudness;
  1146.  
  1147.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1148.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1149.             "Wave_Stereo_8BitIn_NoTime_NoWave:  wave index out of range"));
  1150.         LocalLeftLoudness = State->LeftLoudness;
  1151.         LocalRightLoudness = State->RightLoudness;
  1152.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1153.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1154.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1155.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1156.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1157.             State->WaveTableIndex)]));
  1158.         WaveData = (signed char*)(State->WaveTableMatrix[
  1159.             FastFixed2Int(State->WaveTableIndex)]);
  1160.  
  1161.         while (SampleCount > 0)
  1162.             {
  1163.                 signed long                    SamplePoint;
  1164.  
  1165.                 SamplePoint = WaveData[LongLongHighHalf(LocalWaveTableSamplePosition)
  1166.                     & LocalSamplePositionMask];
  1167.                 *(RawBuffer++) += FastFixedTimes8BitTo24Bit(LocalLeftLoudness,SamplePoint);
  1168.                 *(RawBuffer++) += FastFixedTimes8BitTo24Bit(LocalRightLoudness,SamplePoint);
  1169.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1170.                     &LocalWaveTableSamplePositionDifferential);
  1171.                 SampleCount -= 1;
  1172.             }
  1173.  
  1174.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1175.     }
  1176.  
  1177.  
  1178. static void                        Wave_Mono_16BitIn_NoTime_NoWave(WaveTableStateRec* State,
  1179.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1180.     {
  1181.         LongLongRec                    LocalWaveTableSamplePosition;
  1182.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1183.         unsigned long                LocalSamplePositionMask;
  1184.         signed short*                WaveData;
  1185.         FastFixedType                LocalOverallLoudness;
  1186.  
  1187.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1188.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1189.             "Wave_Mono_16BitIn_NoTime_NoWave:  wave index out of range"));
  1190.         LocalOverallLoudness = State->OverallLoudness;
  1191.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1192.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1193.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1194.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1195.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1196.             State->WaveTableIndex)]));
  1197.         WaveData = (signed short*)(State->WaveTableMatrix[
  1198.             FastFixed2Int(State->WaveTableIndex)]);
  1199.  
  1200.         while (SampleCount > 0)
  1201.             {
  1202.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,
  1203.                     WaveData[LongLongHighHalf(LocalWaveTableSamplePosition)
  1204.                     & LocalSamplePositionMask]);
  1205.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1206.                     &LocalWaveTableSamplePositionDifferential);
  1207.                 SampleCount -= 1;
  1208.             }
  1209.  
  1210.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1211.     }
  1212.  
  1213.  
  1214. static void                        Wave_Stereo_16BitIn_NoTime_NoWave(WaveTableStateRec* State,
  1215.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1216.     {
  1217.         LongLongRec                    LocalWaveTableSamplePosition;
  1218.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1219.         unsigned long                LocalSamplePositionMask;
  1220.         signed short*                WaveData;
  1221.         FastFixedType                LocalLeftLoudness;
  1222.         FastFixedType                LocalRightLoudness;
  1223.  
  1224.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1225.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1226.             "Wave_Stereo_16BitIn_NoTime_NoWave:  wave index out of range"));
  1227.         LocalLeftLoudness = State->LeftLoudness;
  1228.         LocalRightLoudness = State->RightLoudness;
  1229.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1230.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1231.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1232.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1233.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1234.             State->WaveTableIndex)]));
  1235.         WaveData = (signed short*)(State->WaveTableMatrix[
  1236.             FastFixed2Int(State->WaveTableIndex)]);
  1237.  
  1238.         while (SampleCount > 0)
  1239.             {
  1240.                 signed long                    SamplePoint;
  1241.  
  1242.                 SamplePoint = WaveData[LongLongHighHalf(LocalWaveTableSamplePosition)
  1243.                     & LocalSamplePositionMask];
  1244.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,SamplePoint);
  1245.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,SamplePoint);
  1246.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1247.                     &LocalWaveTableSamplePositionDifferential);
  1248.                 SampleCount -= 1;
  1249.             }
  1250.  
  1251.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1252.     }
  1253.  
  1254.  
  1255. static void                        Wave_Mono_8BitIn_YesTime_NoWave(WaveTableStateRec* State,
  1256.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1257.     {
  1258.         LongLongRec                    LocalWaveTableSamplePosition;
  1259.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1260.         unsigned long                LocalSamplePositionMask;
  1261.         signed char*                WaveData;
  1262.         FastFixedType                LocalOverallLoudness;
  1263.  
  1264.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1265.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1266.             "Wave_Mono_8BitIn_YesTime_NoWave:  wave index out of range"));
  1267.         LocalOverallLoudness = State->OverallLoudness;
  1268.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1269.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1270.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1271.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1272.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1273.             State->WaveTableIndex)]));
  1274.         WaveData = (signed char*)(State->WaveTableMatrix[
  1275.             FastFixed2Int(State->WaveTableIndex)]);
  1276.  
  1277.         while (SampleCount > 0)
  1278.             {
  1279.                 FastFixedType                LeftWeight;
  1280.                 long                                ArraySubscript;
  1281.                 signed long                    LeftValue;
  1282.                 signed long                    RightValue;
  1283.  
  1284.                 /* naive anti-aliasing worked as follows: */
  1285.                 /*  L = left sample, R = right sample, F = fractional portion of index */
  1286.                 /*  S = L(1-F) + RF */
  1287.                 /*  Craig Peeper suggested the following optimization: */
  1288.                 /*  L(1-F)+RF ==> L-LF+RF ==> L+RF-LF ==> L+F(R-L) */
  1289.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1290.                     >> (32 - FASTFIXEDPRECISION);
  1291.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1292.                     & LocalSamplePositionMask;
  1293.                 /* L+F(R-L) */
  1294.                 LeftValue = ((signed long)WaveData[ArraySubscript]) << 8; /* convert to 16-bit */
  1295.                 RightValue = ((signed long)WaveData[ArraySubscript + 1]) << 8; /* to 16-bit */
  1296.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,LeftValue
  1297.                     + ((LeftWeight * (RightValue - LeftValue)) >> 15));
  1298.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1299.                     &LocalWaveTableSamplePositionDifferential);
  1300.                 SampleCount -= 1;
  1301.             }
  1302.  
  1303.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1304.     }
  1305.  
  1306.  
  1307. static void                        Wave_Stereo_8BitIn_YesTime_NoWave(WaveTableStateRec* State,
  1308.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1309.     {
  1310.         LongLongRec                    LocalWaveTableSamplePosition;
  1311.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1312.         unsigned long                LocalSamplePositionMask;
  1313.         signed char*                WaveData;
  1314.         FastFixedType                LocalLeftLoudness;
  1315.         FastFixedType                LocalRightLoudness;
  1316.  
  1317.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1318.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1319.             "Wave_Stereo_8BitIn_YesTime_NoWave:  wave index out of range"));
  1320.         LocalLeftLoudness = State->LeftLoudness;
  1321.         LocalRightLoudness = State->RightLoudness;
  1322.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1323.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1324.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1325.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1326.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1327.             State->WaveTableIndex)]));
  1328.         WaveData = (signed char*)(State->WaveTableMatrix[
  1329.             FastFixed2Int(State->WaveTableIndex)]);
  1330.  
  1331.         while (SampleCount > 0)
  1332.             {
  1333.                 FastFixedType                LeftWeight;
  1334.                 long                                ArraySubscript;
  1335.                 signed long                    LeftValue;
  1336.                 signed long                    RightValue;
  1337.                 signed long                    CombinedValue;
  1338.  
  1339.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1340.                     >> (32 - FASTFIXEDPRECISION);
  1341.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1342.                     & LocalSamplePositionMask;
  1343.                 /* L+F(R-L) */
  1344.                 LeftValue = ((signed long)WaveData[ArraySubscript]) << 8; /* convert to 16-bit */
  1345.                 RightValue = ((signed long)WaveData[ArraySubscript + 1]) << 8; /* to 16-bit */
  1346.                 CombinedValue = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  1347.                 /* Oh, Please Mr. Compiler, LeftValue and RightValue are dead now!!! */
  1348.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,CombinedValue);
  1349.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,CombinedValue);
  1350.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1351.                     &LocalWaveTableSamplePositionDifferential);
  1352.                 SampleCount -= 1;
  1353.             }
  1354.  
  1355.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1356.     }
  1357.  
  1358.  
  1359. static void                        Wave_Mono_16BitIn_YesTime_NoWave(WaveTableStateRec* State,
  1360.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1361.     {
  1362.         LongLongRec                    LocalWaveTableSamplePosition;
  1363.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1364.         unsigned long                LocalSamplePositionMask;
  1365.         signed short*                WaveData;
  1366.         FastFixedType                LocalOverallLoudness;
  1367.  
  1368.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1369.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1370.             "Wave_Mono_16BitIn_YesTime_NoWave:  wave index out of range"));
  1371.         LocalOverallLoudness = State->OverallLoudness;
  1372.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1373.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1374.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1375.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1376.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1377.             State->WaveTableIndex)]));
  1378.         WaveData = (signed short*)(State->WaveTableMatrix[
  1379.             FastFixed2Int(State->WaveTableIndex)]);
  1380.  
  1381.         while (SampleCount > 0)
  1382.             {
  1383.                 FastFixedType                LeftWeight;
  1384.                 long                                ArraySubscript;
  1385.                 signed long                    LeftValue;
  1386.                 signed long                    RightValue;
  1387.  
  1388.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1389.                     >> (32 - FASTFIXEDPRECISION);
  1390.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1391.                     & LocalSamplePositionMask;
  1392.                 /* L+F(R-L) */
  1393.                 LeftValue = WaveData[ArraySubscript];
  1394.                 RightValue = WaveData[ArraySubscript + 1];
  1395.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,LeftValue
  1396.                     + ((LeftWeight * (RightValue - LeftValue)) >> 15));
  1397.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1398.                     &LocalWaveTableSamplePositionDifferential);
  1399.                 SampleCount -= 1;
  1400.             }
  1401.  
  1402.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1403.     }
  1404.  
  1405.  
  1406. static void                        Wave_Stereo_16BitIn_YesTime_NoWave(WaveTableStateRec* State,
  1407.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1408.     {
  1409.         LongLongRec                    LocalWaveTableSamplePosition;
  1410.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1411.         unsigned long                LocalSamplePositionMask;
  1412.         signed short*                WaveData;
  1413.         FastFixedType                LocalLeftLoudness;
  1414.         FastFixedType                LocalRightLoudness;
  1415.  
  1416.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1417.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1418.             "Wave_Stereo_16BitIn_YesTime_NoWave:  wave index out of range"));
  1419.         LocalLeftLoudness = State->LeftLoudness;
  1420.         LocalRightLoudness = State->RightLoudness;
  1421.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1422.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1423.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1424.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1425.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1426.             State->WaveTableIndex)]));
  1427.         WaveData = (signed short*)(State->WaveTableMatrix[
  1428.             FastFixed2Int(State->WaveTableIndex)]);
  1429.  
  1430.         while (SampleCount > 0)
  1431.             {
  1432.                 FastFixedType                LeftWeight;
  1433.                 long                                ArraySubscript;
  1434.                 signed long                    LeftValue;
  1435.                 signed long                    RightValue;
  1436.                 signed long                    CombinedValue;
  1437.  
  1438.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1439.                     >> (32 - FASTFIXEDPRECISION);
  1440.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1441.                     & LocalSamplePositionMask;
  1442.                 /* L+F(R-L) */
  1443.                 LeftValue = WaveData[ArraySubscript];
  1444.                 RightValue = WaveData[ArraySubscript + 1];
  1445.                 CombinedValue = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  1446.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,CombinedValue);
  1447.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,CombinedValue);
  1448.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1449.                     &LocalWaveTableSamplePositionDifferential);
  1450.                 SampleCount -= 1;
  1451.             }
  1452.  
  1453.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1454.     }
  1455.  
  1456.  
  1457. static void                        Wave_Mono_8BitIn_YesTime_YesWave(WaveTableStateRec* State,
  1458.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1459.     {
  1460.         LongLongRec                    LocalWaveTableSamplePosition;
  1461.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1462.         unsigned long                LocalSamplePositionMask;
  1463.         signed char*                WaveData0;
  1464.         signed char*                WaveData1;
  1465.         FastFixedType                Wave0Weight;
  1466.         FastFixedType                LocalOverallLoudness;
  1467.  
  1468.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1469.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1470.             "Wave_Mono_8BitIn_YesTime_YesWave:  wave index out of range"));
  1471.         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  1472.             {
  1473.                 /* this is done in case the wave table index is at the maximum, in which */
  1474.                 /* case there is no table+1 to interpolate with. */
  1475.                 Wave_Mono_8BitIn_YesTime_NoWave(State,SampleCount,RawBuffer);
  1476.                 return;
  1477.             }
  1478.         LocalOverallLoudness = State->OverallLoudness;
  1479.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1480.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1481.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1482.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1483.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1484.             State->WaveTableIndex)]));
  1485.         WaveData0 = (signed char*)(State->WaveTableMatrix[
  1486.             FastFixed2Int(State->WaveTableIndex)]);
  1487.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1488.             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1489.             State->WaveTableIndex) + 1]));
  1490.         WaveData1 = (signed char*)(State->WaveTableMatrix[
  1491.             FastFixed2Int(State->WaveTableIndex) + 1]);
  1492.         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  1493.  
  1494.         while (SampleCount > 0)
  1495.             {
  1496.                 FastFixedType                LeftWeight;
  1497.                 long                                ArraySubscript;
  1498.                 signed long                    Left0Value;
  1499.                 signed long                    Right0Value;
  1500.                 signed long                    Left1Value;
  1501.                 signed long                    Right1Value;
  1502.                 FastFixedType                Wave0Temp;
  1503.  
  1504.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1505.                     >> (32 - FASTFIXEDPRECISION);
  1506.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1507.                     & LocalSamplePositionMask;
  1508.                 /* L+F(R-L) -- applied twice */
  1509.                 Left0Value = ((signed long)WaveData0[ArraySubscript]) << 8; /* convert to 16-bit */
  1510.                 Right0Value = ((signed long)WaveData0[ArraySubscript + 1]) << 8; /* to 16-bit */
  1511.                 Left1Value = ((signed long)WaveData1[ArraySubscript]) << 8; /* convert to 16-bit */
  1512.                 Right1Value = ((signed long)WaveData1[ArraySubscript + 1]) << 8; /* to 16-bit */
  1513.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1514.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,
  1515.                     Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight * (Right1Value
  1516.                     - Left1Value)) >> 15) - Wave0Temp)) >> 15));
  1517.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1518.                     &LocalWaveTableSamplePositionDifferential);
  1519.                 SampleCount -= 1;
  1520.             }
  1521.  
  1522.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1523.     }
  1524.  
  1525.  
  1526. static void                        Wave_Stereo_8BitIn_YesTime_YesWave(WaveTableStateRec* State,
  1527.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1528.     {
  1529.         LongLongRec                    LocalWaveTableSamplePosition;
  1530.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1531.         unsigned long                LocalSamplePositionMask;
  1532.         signed char*                WaveData0;
  1533.         signed char*                WaveData1;
  1534.         FastFixedType                Wave0Weight;
  1535.         FastFixedType                LocalLeftLoudness;
  1536.         FastFixedType                LocalRightLoudness;
  1537.  
  1538.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1539.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1540.             "Wave_Stereo_8BitIn_YesTime_YesWave:  wave index out of range"));
  1541.         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  1542.             {
  1543.                 /* this is done in case the wave table index is at the maximum, in which */
  1544.                 /* case there is no table+1 to interpolate with. */
  1545.                 Wave_Stereo_8BitIn_YesTime_NoWave(State,SampleCount,RawBuffer);
  1546.                 return;
  1547.             }
  1548.         LocalLeftLoudness = State->LeftLoudness;
  1549.         LocalRightLoudness = State->RightLoudness;
  1550.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1551.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1552.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1553.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1554.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1555.             State->WaveTableIndex)]));
  1556.         WaveData0 = (signed char*)(State->WaveTableMatrix[
  1557.             FastFixed2Int(State->WaveTableIndex)]);
  1558.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1559.             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1560.             State->WaveTableIndex) + 1]));
  1561.         WaveData1 = (signed char*)(State->WaveTableMatrix[
  1562.             FastFixed2Int(State->WaveTableIndex) + 1]);
  1563.         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  1564.  
  1565.         while (SampleCount > 0)
  1566.             {
  1567.                 FastFixedType                LeftWeight;
  1568.                 long                                ArraySubscript;
  1569.                 signed long                    Left0Value;
  1570.                 signed long                    Right0Value;
  1571.                 signed long                    Left1Value;
  1572.                 signed long                    Right1Value;
  1573.                 FastFixedType                Wave0Temp;
  1574.  
  1575.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1576.                     >> (32 - FASTFIXEDPRECISION);
  1577.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1578.                     & LocalSamplePositionMask;
  1579.                 /* L+F(R-L) -- applied twice */
  1580.                 Left0Value = ((signed long)WaveData0[ArraySubscript]) << 8; /* convert to 16-bit */
  1581.                 Right0Value = ((signed long)WaveData0[ArraySubscript + 1]) << 8; /* to 16-bit */
  1582.                 Left1Value = ((signed long)WaveData1[ArraySubscript]) << 8; /* convert to 16-bit */
  1583.                 Right1Value = ((signed long)WaveData1[ArraySubscript + 1]) << 8; /* to 16-bit */
  1584.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1585.                 Wave0Temp = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight * (Right1Value
  1586.                     - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  1587.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,Wave0Temp);
  1588.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,Wave0Temp);
  1589.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1590.                     &LocalWaveTableSamplePositionDifferential);
  1591.                 SampleCount -= 1;
  1592.             }
  1593.  
  1594.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1595.     }
  1596.  
  1597.  
  1598. static void                        Wave_Mono_16BitIn_YesTime_YesWave(WaveTableStateRec* State,
  1599.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1600.     {
  1601.         LongLongRec                    LocalWaveTableSamplePosition;
  1602.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1603.         unsigned long                LocalSamplePositionMask;
  1604.         signed short*                WaveData0;
  1605.         signed short*                WaveData1;
  1606.         FastFixedType                Wave0Weight;
  1607.         FastFixedType                LocalOverallLoudness;
  1608.  
  1609.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1610.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1611.             "Wave_Mono_16BitIn_YesTime_YesWave:  wave index out of range"));
  1612.         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  1613.             {
  1614.                 /* this is done in case the wave table index is at the maximum, in which */
  1615.                 /* case there is no table+1 to interpolate with. */
  1616.                 Wave_Mono_16BitIn_YesTime_NoWave(State,SampleCount,RawBuffer);
  1617.                 return;
  1618.             }
  1619.         LocalOverallLoudness = State->OverallLoudness;
  1620.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1621.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1622.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1623.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1624.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1625.             State->WaveTableIndex)]));
  1626.         WaveData0 = (signed short*)(State->WaveTableMatrix[
  1627.             FastFixed2Int(State->WaveTableIndex)]);
  1628.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1629.             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1630.             State->WaveTableIndex) + 1]));
  1631.         WaveData1 = (signed short*)(State->WaveTableMatrix[
  1632.             FastFixed2Int(State->WaveTableIndex) + 1]);
  1633.         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  1634.  
  1635.         while (SampleCount > 0)
  1636.             {
  1637.                 FastFixedType                LeftWeight;
  1638.                 long                                ArraySubscript;
  1639.                 signed long                    Left0Value;
  1640.                 signed long                    Right0Value;
  1641.                 signed long                    Left1Value;
  1642.                 signed long                    Right1Value;
  1643.                 FastFixedType                Wave0Temp;
  1644.  
  1645.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1646.                     >> (32 - FASTFIXEDPRECISION);
  1647.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1648.                     & LocalSamplePositionMask;
  1649.                 /* L+F(R-L) -- applied twice */
  1650.                 Left0Value = WaveData0[ArraySubscript];
  1651.                 Right0Value = WaveData0[ArraySubscript + 1];
  1652.                 Left1Value = WaveData1[ArraySubscript];
  1653.                 Right1Value = WaveData1[ArraySubscript + 1];
  1654.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1655.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalOverallLoudness,
  1656.                     Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight * (Right1Value
  1657.                     - Left1Value)) >> 15) - Wave0Temp)) >> 15));
  1658.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1659.                     &LocalWaveTableSamplePositionDifferential);
  1660.                 SampleCount -= 1;
  1661.             }
  1662.  
  1663.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1664.     }
  1665.  
  1666.  
  1667. static void                        Wave_Stereo_16BitIn_YesTime_YesWave(WaveTableStateRec* State,
  1668.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1669.     {
  1670.         LongLongRec                    LocalWaveTableSamplePosition;
  1671.         LongLongRec                    LocalWaveTableSamplePositionDifferential;
  1672.         unsigned long                LocalSamplePositionMask;
  1673.         signed short*                WaveData0;
  1674.         signed short*                WaveData1;
  1675.         FastFixedType                Wave0Weight;
  1676.         FastFixedType                LocalLeftLoudness;
  1677.         FastFixedType                LocalRightLoudness;
  1678.  
  1679.         ERROR((State->WaveTableIndex < 0) || (State->WaveTableIndex
  1680.             > Int2FastFixed(State->NumberOfTables - 1)),PRERR(ForceAbort,
  1681.             "Wave_Stereo_16BitIn_YesTime_YesWave:  wave index out of range"));
  1682.         if (FastFixed2Int(State->WaveTableIndex) == State->NumberOfTables - 1)
  1683.             {
  1684.                 /* this is done in case the wave table index is at the maximum, in which */
  1685.                 /* case there is no table+1 to interpolate with. */
  1686.                 Wave_Stereo_16BitIn_YesTime_NoWave(State,SampleCount,RawBuffer);
  1687.                 return;
  1688.             }
  1689.         LocalLeftLoudness = State->LeftLoudness;
  1690.         LocalRightLoudness = State->RightLoudness;
  1691.         LocalWaveTableSamplePosition = State->WaveTableSamplePosition;
  1692.         LocalWaveTableSamplePositionDifferential = State->WaveTableSamplePositionDifferential;
  1693.         LocalSamplePositionMask = State->FramesPerTable - 1;
  1694.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1695.             State->WaveTableIndex)]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1696.             State->WaveTableIndex)]));
  1697.         WaveData0 = (signed short*)(State->WaveTableMatrix[
  1698.             FastFixed2Int(State->WaveTableIndex)]);
  1699.         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1700.             State->WaveTableIndex) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(
  1701.             State->WaveTableIndex) + 1]));
  1702.         WaveData1 = (signed short*)(State->WaveTableMatrix[
  1703.             FastFixed2Int(State->WaveTableIndex) + 1]);
  1704.         Wave0Weight = State->WaveTableIndex & FASTFIXEDFRACTMASK;
  1705.  
  1706.         while (SampleCount > 0)
  1707.             {
  1708.                 FastFixedType                LeftWeight;
  1709.                 long                                ArraySubscript;
  1710.                 signed long                    Left0Value;
  1711.                 signed long                    Right0Value;
  1712.                 signed long                    Left1Value;
  1713.                 signed long                    Right1Value;
  1714.                 FastFixedType                Wave0Temp;
  1715.  
  1716.                 LeftWeight = LongLongLowHalf(LocalWaveTableSamplePosition)
  1717.                     >> (32 - FASTFIXEDPRECISION);
  1718.                 ArraySubscript = LongLongHighHalf(LocalWaveTableSamplePosition)
  1719.                     & LocalSamplePositionMask;
  1720.                 /* L+F(R-L) -- applied twice */
  1721.                 Left0Value = WaveData0[ArraySubscript];
  1722.                 Right0Value = WaveData0[ArraySubscript + 1];
  1723.                 Left1Value = WaveData1[ArraySubscript];
  1724.                 Right1Value = WaveData1[ArraySubscript + 1];
  1725.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1726.                 Wave0Temp = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  1727.                     * (Right1Value - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  1728.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalLeftLoudness,Wave0Temp);
  1729.                 *(RawBuffer++) += FastFixedTimes16BitTo24Bit(LocalRightLoudness,Wave0Temp);
  1730.                 LongLongAdd(&LocalWaveTableSamplePosition,
  1731.                     &LocalWaveTableSamplePositionDifferential);
  1732.                 SampleCount -= 1;
  1733.             }
  1734.  
  1735.         State->WaveTableSamplePosition = LocalWaveTableSamplePosition;
  1736.     }
  1737.  
  1738.  
  1739. static void                        Wave_NoOutput(WaveTableStateRec* State,
  1740.                                                 long SampleCount, largefixedsigned* RawBuffer)
  1741.     {
  1742.     }
  1743.